package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dg.river.R;
import com.dg.river.core.util.EmojiFilterUtils;
import com.dg.river.core.util.TextUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by hy on 2018/10/16
 * Description : 列表dialog 非从底部弹出
 */
public class ListCenterDialog extends Dialog {

    private static RecyclerView recyclerView;
    private static TextView tv_cancel;
    private static TextView tv_title;
    private static TextView tv_message;

    private static ListCenterDialog dialog;

    /**
     * 点击事件
     */
    private static OnItemClickListener mOnItemClickListener;

    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(ListCenterDialog dialog, BaseQuickAdapter adapter, View view, int position);
    }

    /**
     * 设置点击事件
     *
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public ListCenterDialog(Context context) {
        super(context);
    }

    public ListCenterDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private List<ListDialogItemBean> listData;
        private String confirm_btnText;
        private int confirm_btnColor;
        private String cancel_btnText;
        private int cancel_btnColor;
        private boolean cancel;
        private OnClickListener confirm_btnClickListener;
        private OnClickListener cancel_btnClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置是否能够取消
         *
         * @param bool
         * @return
         */
        public Builder setCancle(boolean bool) {
            this.cancel = bool;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the Dialog message from String
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        public Builder setListData(List<ListDialogItemBean> listData) {
            this.listData = listData;
            return this;
        }

        /**
         * Set the negative button and it's listener
         *
         * @param
         * @return
         */
        public Builder setCancelButton(String cancel_btnText, int cancel_btnColor, OnClickListener listener) {
            this.cancel_btnText = cancel_btnText;
            this.cancel_btnColor = cancel_btnColor;
            this.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * 传入0为默认样式 否则为自定义样式
         *
         * @param style
         * @return
         */
        public ListCenterDialog create(int style) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (style == 0) {
                dialog = new ListCenterDialog(context, R.style.dialog_message_normal);
            } else {
                dialog = new ListCenterDialog(context, style);
            }
            View layout = inflater.inflate(R.layout.dialog_list_center, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

            recyclerView = layout.findViewById(R.id.recyclerView);
            tv_cancel = layout.findViewById(R.id.tv_cancel);
            tv_title = layout.findViewById(R.id.tv_title);
            tv_message = layout.findViewById(R.id.tv_message);
            /**
             * 设置显示
             */
            tv_title.setText(TextUtil.isEmptyConvert(title));
            tv_message.setText(TextUtil.isEmptyConvert(message));
            tv_cancel.setText(cancel_btnText);
            if (cancel_btnColor != -1) {
                tv_cancel.setTextColor(context.getResources().getColor(cancel_btnColor));
            }
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cancel_btnClickListener != null) {
                        cancel_btnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                }
            });
            dialog.setContentView(layout);

            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            ListDialogAdapter listDialogAdapter = new ListDialogAdapter(R.layout.dialog_list_item, listData);
            recyclerView.setAdapter(listDialogAdapter);
            listDialogAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(dialog, adapter, view, position);
                    }
                }
            });
            if (!cancel) {
                dialog.setCancelable(false);
            } else {
                dialog.setCancelable(true);
            }
            return dialog;
        }
    }


    /**
     * item内容
     */
    public static class ListDialogItemBean implements Serializable {
        private String text;
        private int textColor;
        private String id;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    /**
     * adapter
     */
    private static class ListDialogAdapter extends BaseQuickAdapter<ListDialogItemBean, BaseViewHolder> {

        public ListDialogAdapter(int layoutResId, @Nullable List<ListDialogItemBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, ListDialogItemBean item) {
            if (!TextUtil.isEmpty(EmojiFilterUtils.filterEmoji(item.getText()))) {
                baseViewHolder.setText(R.id.tv_item_name, EmojiFilterUtils.filterEmoji(item.getText()));
                if (item.getTextColor() != 0) {
                    baseViewHolder.setTextColor(R.id.tv_item_name, mContext.getResources().getColor(item.getTextColor()));
                } else {
                    baseViewHolder.setTextColor(R.id.tv_item_name, mContext.getResources().getColor(R.color.black_141414));
                }
            }
        }
    }
}
